home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- char *buffer [300] ;
-
- main ()
-
- /*
- +-----------------------------------------------+
- | |
- | This is the test program for comparing |
- | the new memory allocation procedures and |
- | those of MSC. To compile for MSC, do: |
- | |
- | cl /c /AL /DMSC *.c |
- | linker |
- | |
- | To compile for the new allocation procs, do: |
- | |
- | cl /c /AL *.c |
- | linker |
- | |
- | For either set, clear the screen and then |
- | run the test program. The MSC test will |
- | perform a series of allocation, reallocation,|
- | free types of activities. After each |
- | activity, a dump of the heap is appended to |
- | a file named 'memory.msc'. The alternate |
- | test will perform the same activities |
- | except it will write the heap information |
- | to a file called 'memory.new'. You may |
- | compare the two files for performance. |
- | |
- | While the test is running, the current size |
- | of various things is displayed on the top |
- | line of the screen (see 'dispsize.c' for |
- | expanation). |
- | |
- +-----------------------------------------------+
- */
-
- {
- int i, j, max ;
- void *malloc () ;
- void *realloc () ;
- void free () ;
- char *bufptr ;
- int mem ;
-
- #ifndef MSC
- ReduceDefaultData () ; /* Reduce default data size */
- printf ( "\n** Non-MSC memory test **\n" ) ;
- #else
- extern size_t _amblksiz ;
- _amblksiz = 1024 ; /* Set allocation block size the same
- for both tests. */
- printf ( "\n** MSC memory test **\n" ) ;
-
- /* NOTE: a similar effect to ReduceDefaultData can be achieved
- for MSC by running 'exemod' on this test program to
- set the max paragraph size to the min paragraph size. */
- #endif
-
- max = 0 ;
- srand ( 12345) ; /* Init. random number generator */
- /* This can be changed to
- different values to get
- varying results. */
-
- /* First do some straight memory allocations */
-
- printf ( "Starting Allocation\n" ) ;
- for ( i = 0 ; i < 300 ; i++ ) {
- mem = randnum ( 1, 1024 ) ; /* Generate allocation size */
- /* between 1 and 1024 */
- buffer [i] = (char *) malloc ( mem ) ;
- if ( buffer [i] == NULL ) {
- printf ( "Unable to allocate memory buffer %d\n", i ) ;
- break ;
- } ;
- DisplaySize () ; /* Display program size */
- max++ ;
- } ;
- DumpHeap ( "After all Allocations" ) ;
-
- /* Next, reallocate blocks to different sizes starting with
- the last block allocated and working down to the lowest */
-
- printf ( "Starting Reallocation - Top down\n" ) ;
- for ( j = max-1 ; j <= 0 ; j++ ) {
- mem = randnum ( 1, 1024 ) ; /* Generate reallocation size */
- /* between 1 and 1024 */
- if ( buffer [j] != NULL ) {
- bufptr = (char *) realloc ( buffer [j], mem ) ;
- if ( bufptr == NULL )
- printf ( "Unable to realloc buffer %d\n", j ) ;
- else
- buffer [j] = bufptr ;
- } ;
- DisplaySize () ; /* Display program size */
- } ;
- DumpHeap ( "After all Reallocations - Top Down" ) ;
-
- /* Next, reallocate the blocks starting with the lowest
- block and working upwards. */
-
- printf ( "Starting Reallocation - Bottom Up\n" ) ;
- for ( j = 0 ; j < max ; j++ ) {
- mem = randnum ( 1, 1024 ) ; /* Generate reallocation size */
- if ( buffer [j] != NULL ) {
- bufptr = (char *) realloc ( buffer [j], mem ) ;
- if ( bufptr == NULL )
- printf ( "Unable to realloc buffer %d\n", j ) ;
- else
- buffer [j] = bufptr ;
- } ;
- DisplaySize () ; /* Display program size */
- } ;
- DumpHeap ( "After all Reallocations - Bottom Up" ) ;
-
- /* Lastly, free all allocated blocks starting with the
- last block in buffer array (which may now not be the
- last block in memory). */
-
- printf ( "Starting Free\n" ) ;
- while ( --max >= 0 ) {
- if ( buffer [max] != NULL )
- free ( buffer [max] ) ;
- #ifndef MSC
- ReduceAllocation ( 1 ) ; /* Reduce allocation to minimum */
- #endif
- DisplaySize () ; /* Display program size */
- } ;
- DumpHeap ( "Finish" ) ;
- }
-
- randnum ( start, end )
-
- int start ;
- int end ;
-
- /*
- +------------------------------------------------------+
- | |
- | This procedure generates a random integer number |
- | in the range: |
- | |
- | start <= randnum <= end |
- | |
- +------------------------------------------------------+
- */
-
- {
- int number ;
- int range ;
- long junk ;
-
- range = end - start + 1 ;
- number = rand () ;
- junk = number ;
- junk = ((long) range * junk) / 077777 + (long) start ;
- number = (int) junk ;
- if ( number < start ) number = start ;
- if ( number > end ) number = end ;
- return ( number ) ;
- }